﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        function SetCookie() {

            var date = new Date(); //1970
            var day = document.getElementById("txtExpires").value * 1;
            date.setTime(date.getTime() + 1000 * 60 * 60 * 24 * day);

            document.cookie = "UserName=" + document.getElementById("txtUserName").value + "; expires=" + date.toGMTString();
            document.cookie = "Pwd=" + document.getElementById("txtPwd").value + "; expires=" + date.toGMTString();
        }

        function OnPageLoad() {
            var kvDict = new Array();
            //var str = "UserName=abc; Pwd=123";
            var str = document.cookie;
            if (str) {
                var arr = str.split("; ");
                for (var i = 0; i < arr.length; i++) {
                    var kv = arr[i].split("=");
                    kvDict[kv[0]] = kv[1];
                }

                document.getElementById("txtUserName").value = kvDict["UserName"];
                document.getElementById("txtPwd").value = kvDict["Pwd"];
            }
        }

        function ClearCookie() {
            var date = new Date(1970, 0, 1);
            document.cookie = "UserName=; expires=" + date.toGMTString();
            document.cookie = "Pwd=; expires=" + date.toGMTString();
        }
    
    </script>
</head>
<body onload="OnPageLoad();">
    <input id="txtUserName" type="text" />
    <br />
    <input id="txtPwd" type="text" />
    <br />
    <input type="button" value="登陆" onclick="SetCookie();" />
    <br />
    用户名密码<input id="txtExpires" type="text" />天内有效
    <br />
    <input type="button" value="清除Cookie" onclick="ClearCookie();" />
</body>
</html>
